<?php

namespace app\controller;

//商店控制器
use app\service\ShopService;
use think\facade\Db;

class  ShopController extends Base
{

    //获取商店列表
    public function list()
    {
        $res = (new ShopService())->list();
        return json($res);
    }

    //购买商品
    public function buy()
    {
        try {
            Db::startTrans();
            $user = $this->user;
            $shop_id = $this->request->post('shop_id', '');
            $num = $this->request->post('num', 1);
            $res = (new ShopService())->buy($user, $shop_id, $num);
            if (!$res['code']) {
                Db::rollback();
            } else {
                Db::commit();
            }
        } catch (\Exception $e) {
            Db::rollback();
            return json(apiError($e));
        }
        return json($res);

    }


}
